package TAIC.util;

import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;

public class MergeDataSets {	
	/**
	 * @param args
	 * This program is used to merge binary dataset to more classes dataset
	 * Usage: ~ datasets.txt
	 * Output: the merged dataset svmCorpus and word2Image.txt
	 */
	public static void main(String[] args) throws Exception {
		if ( args.length != 1 ) { 
			System.out.println ( "please input the config file that contains to datasets to includes" ) ;
			return ; 
		}
		(new MergeDataSets ()).work ( args ) ;
	}
	
	public void work ( String args [] ) throws Exception {
		int totalLines = 0 ;
		int totalClasses = 0 ; 
		
		PrintStream svmSet = new PrintStream ( "svmCorpus" ) ;
		PrintStream wordProb = new PrintStream ( "word2image.txt")  ;

		Scanner scanner = new Scanner ( new File ( args [ 0 ] )) ;
		while ( scanner.hasNext() ) {
			String folder = scanner.nextLine().trim() ;
			Scanner s1 = new Scanner ( new File ( folder + "\\svmCorpus")) ;
			totalLines += s1.nextInt() ;
			totalClasses += s1.nextInt() ; 
		}
		scanner.close() ;
		svmSet.println ( totalLines + " " + totalClasses ) ; 
		
		int curClass = 0 ; 
		for ( int i = 0 ; i < 2 ; i ++ ) { 
			scanner = new Scanner ( new File ( args [ 0 ] )) ;
			while ( scanner.hasNext() ) {
				String folder = scanner.nextLine().trim() ;
				Scanner s1 = new Scanner ( new File ( folder + "\\svmCorpus")) ;
				int lines = s1.nextInt() ;
				int classes = s1.nextInt() ; 
				s1.nextLine() ;
				for ( int j = 0 ; j < lines ; j ++ ) {
					int classNo = s1.nextInt() ; 
					String line = s1.nextLine () ; 
					if ( classNo < ( i + 1 )* classes  && classNo >= i * classes ) 
						svmSet.println ( ( classNo % classes + curClass ) + " " + line ) ; 
				}
				s1.close() ;
				curClass += classes ;				
			}
			scanner.close() ;
		}
		
		curClass = 0 ;
		scanner = new Scanner ( new File ( args [ 0 ] )) ;
		while ( scanner.hasNext() ) {
			String folder = scanner.nextLine().trim() ;
			Scanner s1 = new Scanner ( new File ( folder + "\\word2image.txt" )); 
			int classes = s1.nextInt() ; 
			int words = s1.nextInt () ;
			if ( curClass == 0 ) wordProb.println ( totalClasses + " " + words ) ;  
			while ( s1.hasNextInt() ) { 
				int classNo = s1.nextInt() ;
				String line = s1.nextLine() ; 
				wordProb.println ( ( classNo + curClass ) + " " + line ) ;				
			}
			s1.close() ;
			curClass += classes ; 
		}
		scanner.close() ;		
		
		svmSet.close() ;
		wordProb.close() ;
	}

}
 